package com.xh.bussiness.system.listener;

import com.alibaba.fastjson.JSONObject;
import com.xh.bussiness.system.model.ExportLog;
import com.xh.bussiness.system.service.IExportLogService;
import com.xh.core.event.ExportExcelEvent;
import com.xh.core.properties.XhProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/*
 * @Author daiwei
 * @Description
 * @Date 2018/8/13 07:21
 **/
@Component
public class ExportExcelListner {

    @Autowired
    private XhProperties xhProperties;
    @Autowired
    private IExportLogService exportLogService;

    @EventListener
    public void export(ExportExcelEvent exportExcelEvent){
        if(!xhProperties.isExportLog()){
            return;
        }

        String exportId=exportExcelEvent.getExportId();
        HttpServletRequest request = exportExcelEvent.getRequest();
        String url=exportExcelEvent.getUrl();
        Integer status=exportExcelEvent.getStatus();
        //request.getParameterMap();

        ExportLog exportLog=exportLogService.getById(exportId);
        if(exportLog==null){
            exportLog=new ExportLog();
            exportLog.setId(exportId);
            exportLog.setRequestUrl(request.getRequestURI());
            exportLog.setRequestParam(JSONObject.toJSONString(request.getParameterMap()));
            exportLog.setDownloadUrl(url);
            exportLog.setStatus(status);
            exportLogService.insert(exportLog);
        }else{
            exportLog.setRequestUrl(request.getRequestURI());
            exportLog.setStatus(status);
            exportLog.setDownloadUrl(url);
            exportLogService.update(exportLog);
        }
    }
}
